﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Volume_Resource_Management
{
    public partial class frmAddSalesForcast : Form
    {
        TextBox[] txtT1 = new TextBox[18];
        Label[] lblT1 = new Label[18];

        TextBox[] txtT2 = new TextBox[18];
        Label[] lblT2 = new Label[18];

        TextBox[] txtT3 = new TextBox[18];
        Label[] lblT3 = new Label[18];

        List<int> t1 = new List<int>();
        List<int> t2 = new List<int>();
        List<int> t3 = new List<int>();

        int storeNo;
        int currentY;

        public frmAddSalesForcast(int sNo, int currentYear)
        {
            InitializeComponent();
            storeNo = sNo;
            currentY = currentYear;
        }

        private void frmAddSalesForcast_Load(object sender, EventArgs e)
        {
            //goals = databaseAccessability.getSalesGoal();

            t1 = databaseAccessability.getTertial(storeNo, 1, currentY);
            t2 = databaseAccessability.getTertial(storeNo, 2, currentY);
            t3 = databaseAccessability.getTertial(storeNo, 3, currentY);

            int t1Start = 35;
            int t2Start = 1;
            int t3Start = 18;

            for (int i = 0; i < 18; i++)
            {
                txtT1[i] = new TextBox();
                txtT2[i] = new TextBox();
                txtT3[i] = new TextBox();

                lblT1[i] = new Label();
                lblT2[i] = new Label();
                lblT3[i] = new Label();

                txtT1[i].Text = t1[i].ToString();
                lblT1[i].Text = t1Start.ToString();

                txtT3[i].Text = t3[i].ToString();
                lblT3[i].Text = t3Start.ToString();
                
                txtT1[i].Width = txtT2[i].Width = txtT3[i].Width = lblT1[i].Width = lblT2[i].Width = lblT3[i].Width = (groupBox1.Width - 10) / 18;
                txtT1[i].Top = txtT2[i].Top = txtT3[i].Top = 45;
                lblT1[i].Top = lblT2[i].Top = lblT3[i].Top = 25;

                txtT1[i].TextAlign = txtT2[i].TextAlign = txtT3[i].TextAlign = HorizontalAlignment.Center;
                lblT1[i].TextAlign = lblT2[i].TextAlign = lblT3[i].TextAlign = ContentAlignment.TopCenter;

                txtT1[i].Left = txtT2[i].Left = txtT3[i].Left = lblT1[i].Left = lblT2[i].Left = lblT3[i].Left = (txtT1[i].Width * i) + 10;

                txtT1[i].Font = txtT2[i].Font = txtT3[i].Font = lblT1[i].Font = lblT2[i].Font = lblT3[i].Font = new Font("Verdana", 8);

                if (!IsOdd(i))
                {
                    txtT1[i].BackColor = Color.LightGray;
                    txtT2[i].BackColor = Color.LightGray;
                    txtT3[i].BackColor = Color.LightGray;
                }

                groupBox1.Controls.Add(txtT1[i]);
                groupBox1.Controls.Add(lblT1[i]);

                groupBox3.Controls.Add(txtT3[i]);
                groupBox3.Controls.Add(lblT3[i]);

                if (i != 17)
                {
                    txtT2[i].Text = t2[i].ToString();
                    lblT2[i].Text = t2Start.ToString();

                    groupBox2.Controls.Add(txtT2[i]);
                    groupBox2.Controls.Add(lblT2[i]);
                }

                t1Start++;
                t2Start++;
                t3Start++;
            }
        }

        public static bool IsOdd(int value)
        {
            return value % 2 != 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < txtT1.Count(); i++)
            {
                databaseAccessability.updateSalesInformation(Convert.ToInt32(lblT1[i].Text), 1, Convert.ToInt32(txtT1[i].Text), currentY);
            }

            for (int i = 0; i < txtT2.Count(); i++)
            {
                if(i != 17)
                    databaseAccessability.updateSalesInformation(Convert.ToInt32(lblT2[i].Text), 2, Convert.ToInt32(txtT2[i].Text), currentY);
            }

            for (int i = 0; i < txtT3.Count(); i++)
            {
                databaseAccessability.updateSalesInformation(Convert.ToInt32(lblT3[i].Text), 3, Convert.ToInt32(txtT3[i].Text), currentY);
            }

            this.Hide();
        }
    }
}
